
package security.DAO;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

/**
 *
 * @author JuanFelipe
 */
public class UserDAO extends SecurityDAO{
    //public Connection conn;

    public UserDAO(Connection conn) {
        super(conn);
    }
   
    @Override
    public void crear( String sql ) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void eliminar( String sql ) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void update( String sql ) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
    public void createUser(String user, int identified,
            String authentication, String defaultTableSpace,
            String temporaryTableSpace, List<String> tableSpaces, 
            List<String> QUOTA, String profile, boolean expires,
            int lock, boolean enableEditions,
            int container) throws SQLException {
        Statement statement = conn.createStatement();
        String sql = "CREATE USER \"" + user + "\" IDENTIFIED ";
        switch (identified) {
            case 1:
                sql += "BY " + authentication+" ";
                break;
            case 2:
                sql += "EXTERNALLY ";
                break;
            case 3:
                sql += "EXTERNALLY AS \'" + authentication + "\' ";
                break;
            case 4:
                sql += "globally ";
                break;
            case 5:
                sql += "GLOBALLY AS \'" + authentication + "\' ";
                break;
        }
        if(!defaultTableSpace.equals("")){
            sql += "DEFAULT TABLESPACE "+defaultTableSpace+" ";
            sql += "QUOTA unlimited on "+ defaultTableSpace+" ";
        }
        if( !temporaryTableSpace.equals("") ){
            sql += "TEMPORARY TABLESPACE "+temporaryTableSpace+" ";
        }
        int quota = 0;
        for (String tablespace : tableSpaces){
            sql +="QUOTA "+QUOTA.get(quota)+" ON "+tablespace+" ";
            quota++;
        }
        if (!profile.equals("")){
            sql += "PROFILE \""+profile+"\" ";
        }
        if(expires){
            sql += "PASSWORD EXPIRE ";
        }
        switch(lock){
            case 1:
                sql += "ACCOUNT LOCK ";
                break;
            case 0:
                sql += "ACCOUNT UNLOCK ";
                break;
            default:
                break;
        }
        /*if(enableEditions){
            sql += "ENABLE EDITIONS ";
        }*/
        /*switch(container){
                case 1:
                    sql += "CONTAINER = CURRENT ";
                    break;
                case 2:
                    sql += "CONTAINER = ALL ";
                    break;
                default:
                    break;
        }*/
        //sql +=";";
        System.out.println("sql: "+sql);
        statement.execute(sql);
        
    }
    public void dropUser(String user, boolean cascade)throws SQLException{
        Statement statement = conn.createStatement();
        String sql = "DROP USER "+user+" ";
        if(cascade){
            sql += "CASCADE";
        }
        //sql+=";";
        statement.execute(sql);
    }
    
}
